package com.wang.springframework.beans.factory.support;

import com.wang.springframework.beans.BeansException;
import com.wang.springframework.beans.factory.config.BeanDefinition;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author wangjianhua
 * @Description 代理简单实现
 * @date 2021/8/23/023 16:03
 **/
public class SimpleInstantiationStrategy implements InstantiationStrategy{


    @Override
    public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor<?> ctor, Object[] args) {
        Class<?> clazz = beanDefinition.getBeanClass();
        try
        {
        if(null != ctor) {

            return clazz.getDeclaredConstructor(ctor.getParameterTypes()).newInstance(args);

        }
        else {
            return clazz.getDeclaredConstructor().newInstance();
        }
        }
        catch (InstantiationException | IllegalAccessException | InvocationTargetException| NoSuchMethodException e)
        {
                throw new BeansException("Failed to instantiate ["+clazz.getName()+"]",e);
        }
    }
}
